******************************************************************************************************************
*		
*		Tile: Political Agency, Election Quality, and Corruption
*
*		Authors: Nelson Ruiz-Guarin and Miguel R. Rueda
* 
*		Description: It generates Figure 3 in appendix
*
******************************************************************************************************************

use "C:\Users\mrueda\Documents\Emory\Papers\Corruption\Data\Colombia\replication_data.dta", clear
set more off

br muni_code year lz_pob_mesa_f z_pob_mesa_f  

local counter = 0

*alcalde   

foreach var in alcalde {
forvalues v=10(10)200 {
	quietly ivreg2 `var' (l_vb_r=lz_pob_mesa) lpotencial  if (year==2003 | year==2007) & abs(forcing)<`v', cluster(muni_code)

	local counter = `counter'+1
	mat b = e(b)
	local beta_`counter' = round(b[1,1],0.001)
	mat v = e(V)
	local se = sqrt(v[1,1])
	local uci_`counter' = (`beta_`counter'' + (1.645*`se'))
	local lci_`counter' = (`beta_`counter'' + (-1.645*`se'))
	local bw_`counter' = `v'
*	local N_`counter' = e(N)
}
matrix graph = J(`counter',4,.)
forvalues i=1/`counter' {
	mat graph[`i',1] = `bw_`i''
	mat graph[`i',2] = `beta_`i''
	mat graph[`i',3] = `lci_`i''
	mat graph[`i',4] = `uci_`i''
}

mata : st_matrix("graph", sort(st_matrix("graph"), 1))
preserve
clear 
svmat graph
* graphs
local line_2 = round(`x_line2',0.001)
*attacks
tw (sc graph2 graph1) (rcap graph4 graph3 graph1, lcolor(black)), title("`var'") subtitle("") ///
xtitle("Bandwidth") ytitle("Point Estimate") xli(`bw') xlabel(#8) xscale(range(0.04 0.06)) legend(off)  graphregion(fcolor(white))  yline(0)
}
foreach var in alcalde_guilty {
forvalues v=10(10)200 {
	quietly ivreg2 `var' (l_vb_r=lz_pob_mesa) lpotencial  if (year==2003 | year==2007) & abs(forcing)<`v', cluster(muni_code)

	local counter = `counter'+1
	mat b = e(b)
	local beta_`counter' = round(b[1,1],0.001)
	mat v = e(V)
	local se = sqrt(v[1,1])
	local uci_`counter' = (`beta_`counter'' + (1.645*`se'))
	local lci_`counter' = (`beta_`counter'' + (-1.645*`se'))
	local bw_`counter' = `v'
*	local N_`counter' = e(N)
}
matrix graph = J(`counter',4,.)
forvalues i=1/`counter' {
	mat graph[`i',1] = `bw_`i''
	mat graph[`i',2] = `beta_`i''
	mat graph[`i',3] = `lci_`i''
	mat graph[`i',4] = `uci_`i''
}

mata : st_matrix("graph", sort(st_matrix("graph"), 1))
preserve
clear 
svmat graph
* graphs
local line_2 = round(`x_line2',0.001)
*attacks
tw (sc graph2 graph1) (rcap graph4 graph3 graph1, lcolor(black)), title("`var'") subtitle("") ///
xtitle("Bandwidth") ytitle("Point Estimate") xli(`bw') xlabel(#8) xscale(range(0.04 0.06)) legend(off)  graphregion(fcolor(white))  yline(0)
}
foreach var in alcalde_impeached {
forvalues v=10(10)200 {
	quietly ivreg2 `var' (l_vb_r=lz_pob_mesa) lpotencial  if (year==2003 | year==2007) & abs(forcing)<`v', cluster(muni_code)

	local counter = `counter'+1
	mat b = e(b)
	local beta_`counter' = round(b[1,1],0.001)
	mat v = e(V)
	local se = sqrt(v[1,1])
	local uci_`counter' = (`beta_`counter'' + (1.645*`se'))
	local lci_`counter' = (`beta_`counter'' + (-1.645*`se'))
	local bw_`counter' = `v'
*	local N_`counter' = e(N)
}
matrix graph = J(`counter',4,.)
forvalues i=1/`counter' {
	mat graph[`i',1] = `bw_`i''
	mat graph[`i',2] = `beta_`i''
	mat graph[`i',3] = `lci_`i''
	mat graph[`i',4] = `uci_`i''
}

mata : st_matrix("graph", sort(st_matrix("graph"), 1))
preserve
clear 
svmat graph
* graphs
local line_2 = round(`x_line2',0.001)
*attacks
tw (sc graph2 graph1) (rcap graph4 graph3 graph1, lcolor(black)), title("`var'") subtitle("") ///
xtitle("Bandwidth") ytitle("Point Estimate") xli(`bw') xlabel(#8) xscale(range(0.04 0.06)) legend(off)  graphregion(fcolor(white))  yline(0)
}
